import { getStorage, setStorage } from '@/utils/storage'
import { Toast } from 'vant'
import { login } from '@/api'
const store = {
  namespaced: true,
  state: {
    loginState: getStorage('loginState') === 'true' || false,
    userid: getStorage('userid') || '',
    token: getStorage('token') || ''
  },
  actions: {
    async login (context, data) {
      const res = await login(data)
      if (res.data.code === '10004') {
        Toast('请您先注册帐户')
      } else if (res.data.code === '10005') {
        Toast('密码错误')
      } else {
        Toast('登录成功')
        context.commit('change_login_state', true)
        context.commit('change_userid', res.data.data.userid)
        context.commit('change_token', res.data.data.token)
        setStorage('loginState', true)
        setStorage('userid', res.data.data.userid)
        setStorage('token', res.data.data.token)
      }
    }
  },
  mutations: {
    change_login_state (state, data) {
      state.loginState = data
    },
    change_userid (state, data) {
      state.userid = data
    },
    change_token (state, data) {
      state.token = data
    }
  },
  getters: {}
}

export default store
